package com.sls.web.manager.sys.service.impl;

import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;
import com.sls.web.manager.sys.pojo.Role;
import com.sls.web.manager.sys.querybean.RoleInfoQuery;
import com.sls.web.manager.sys.service.RoleService;

@Service("roleService")
public class RoleServiceImpl extends SuperService implements RoleService {
	private static Logger log = Logger.getLogger(OperatorServiceImpl.class);

	@Override
	public boolean addRole(Role role, UserInfoVO userinfo) throws Exception {
		try {
			String ip = userinfo.getCurrentIp();
			Head head = new Head();
			head.setIp(ip);
			head.setServiceCode(Constants.ADD_ROLE);
			head.setUserId(userinfo.getUserId());
			SocketDTO dto = super.send(head, role);
			if (TransmissionCode.SUCCESS.getCode().equals(
					dto.getHead().getResponseCode()))
				return true;
			else
				new ManagerException(head.getResponseMessage());
		} catch (Exception e) {
			log.error("错误", e);
			throw e;
		}
		return false;

	}

	@Override
	public Role queryRoleInfoById(String id, UserInfoVO userinfo)
			throws Exception {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_ROLE_BY_ID);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		Role ro = new Role();
		ro.setId(id);
		SocketDTO dto = super.send(head, ro);
		String body = dto.getBody();
		Role role = gson.fromJson(body, Role.class);
		return role;
	}

	@Override
	public boolean updateRole(Role role, UserInfoVO userinfo) throws Exception {
		try {
			String ip = userinfo.getCurrentIp();
			Head head = new Head();
			head.setIp(ip);
			head.setRequestId(UUID.randomUUID().toString());
			head.setServiceCode(Constants.UPDATE_ROLE);
			head.setUserId(userinfo.getUserId());
			SocketDTO dto = super.send(head, role);
			if (TransmissionCode.SUCCESS.getCode().equals(
					dto.getHead().getResponseCode()))
				return true;
			else
				new ManagerException(head.getResponseMessage());
		} catch (Exception e) {
			log.error("错误", e);
			throw e;
		}
		return false;
	}

	@Override
	public Pagination<?> queryRole(RoleInfoQuery rQuery, UserInfoVO userinfo)
			throws Exception {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_ALL_ROLE);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = new SocketDTO();
		dto.setHead(head);
		dto = super.send(head, rQuery);
		String body = dto.getBody();
		Pagination<Role> role = gson.fromJson(body,
				new TypeToken<Pagination<Role>>() {
				}.getType());

		return role;
	}

	@Override
	public boolean delete(String id, UserInfoVO userinfo) throws Exception {
		try {
			String ip = userinfo.getCurrentIp();
			Head head = new Head();
			head.setIp(ip);
			head.setRequestId(UUID.randomUUID().toString());
			head.setServiceCode(Constants.DELETE_ROLE_BY_ID);
			Role ro = new Role();
			ro.setId(id);
			head.setUserId(userinfo.getUserId());
			SocketDTO dto = super.send(head, ro);
			if (TransmissionCode.SUCCESS.getCode().equals(
					dto.getHead().getResponseCode()))
				return true;
			else
				new ManagerException(head.getResponseMessage());
		} catch (Exception e) {
			log.error("错误", e);
			throw e;
		}
		return false;
	}

}
